<?php

namespace App\Http\Controllers\Admin;

use App\Http\Models\Manager;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ManagerController extends Controller
{
    //登录页
    public function login(Request $request){
        if(\Auth::guard('back')->check()){
            return redirect('admin/index/index');
        }
        if($request->isMethod('post')){
            $data = $request->all();
            $rules = [
                'mg_name'   => 'required',
                'password'  => 'required',
                'captcha'   => 'required|captcha'
            ];
            $notices = [
                'mg_name.required'  => '用户名不能为空',
                'password.required' => '密码不能为空',
                'captcha.required'  => '验证码不能为空',
                'captcha.captcha'   => '验证错误',
            ];
            $validator = \Validator::make($data,$rules,$notices);
            if($validator->passes()){
                //$request->only() 取出需要的数据子集
                if(\Auth::guard('back')->attempt($request->only(['mg_name','password']),$request->input('online'))){
                    //如果验证通过
                    return redirect('admin/index/index');
                }else{
                    return redirect('admin/manager/login')
                        ->withErrors(['errorinfo'=>'用户名或密码错误'])
                        ->withInput();
                }
            }else{
                return redirect('/admin/manager/login')
                        ->withErrors($validator)
                        ->withInput();
            }
        }
        return view('admin.manager.login');
    }

    //管理员列表
    public function index(){
        $info = Manager::with('role')
            ->get();
//        dd($info);
        $info = $info->toArray();
//        dd($info);
         return view('admin.manager.index',compact('info'));
    }
    
}
